﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using babydealModel;

public partial class dn_chi_tiet_khuyen_mai : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ListView1_DataBound(object sender, EventArgs e)
    {
        int rtime = int.Parse(HidRemainingTime.Value);
        if (rtime >= 0)
        {
            TimeSpan ts = new TimeSpan(0, 0, rtime);
            HidH.Value = Convert.ToString(ts.Hours + ts.Days * 24);
            HidM.Value = Convert.ToString(ts.Minutes);
            HidS.Value = Convert.ToString(ts.Seconds);
        }
        else
        {
            HidH.Value = Convert.ToString(0);
            HidM.Value = Convert.ToString(0);
            HidS.Value = Convert.ToString(0);
        }
        int remain = 0;
        int bought = 0;
        using (babydealEntities context = new babydealEntities())
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            mua_hang muaHang = new mua_hang();
            var rsMH = from s in context.mua_hang
                       where s.id_khuyen_mai == id
                       select s;
            khuyen_mai khuyenMai = new khuyen_mai();
            var rsKM = from s in context.khuyen_mai
                       where s.id == id
                       select s.so_luong_toi_da;
            bought = rsMH.Count();
            remain = rsKM.First().Value - bought;
        }

        foreach (ListViewDataItem item in ListView1.Items)
        {
            Label lbHours = (Label)item.FindControl("lbHours");
            Label lbMinutes = (Label)item.FindControl("lbMinutes");
            Label lbSeconds = (Label)item.FindControl("lbSeconds");
            HidHoursLabelID.Value = lbHours.ClientID;
            HidMinutesLabelID.Value = lbMinutes.ClientID;
            HidSecondsLabelID.Value = lbSeconds.ClientID;

            Label lbBought = (Label)item.FindControl("lbBought");
            Label lbRemain = (Label)item.FindControl("lbRemain");
            Label lbCanBuy = (Label)item.FindControl("lbCanBuy");
            if (remain > 0)
            {
                lbBought.Text = bought.ToString() + " người đã mua";
                lbRemain.Text = "Chỉ còn lại <strong>" + remain.ToString() + "</strong> cơ hội, mua ngay";
                lbCanBuy.Text = "<img src='../themes/blue/images/deal-buy-succ.gif'>Vẫn còn mua được...";
            }
            else
            {
                lbBought.Text = bought.ToString() + " người đã mua";
                lbRemain.Text = "<strong>Rất tiếc bạn đã chậm tay</strong>";
                lbCanBuy.Text = "<img src='themes/blue/images/deal-buy-succ.gif'>Không còn hàng rồi T_T";
            }
        }
    }
}